package system.dao;

import java.sql.ResultSet;
import java.util.ArrayList;

import system.bean.DocRelation;
import util.DBConnect;
import util.ObjectRelationMappingUtil;

public class DocRelationDao implements DaoInterFace {

	@Override
	public boolean delete(Object bean) {
		boolean success = false;
		DocRelation docRelation = (DocRelation)bean;
		String sql = "delete from "+docRelation.getTableName()+" where mainDocId= "+docRelation.getMainDocId() +" and relatedDocId = "+docRelation.getRelatedDocId() ;
		DBConnect dbc = null;
		try{
			dbc = new DBConnect(sql);
			dbc.executeUpdate();
			success = true;
		}catch(Exception e){
			System.out.println("delete() Exception in "+this.getClass()+":"+e.getMessage());
		}finally{
			if(dbc!=null)
				dbc.close();
		}
		return success;
	}

	@Override
	public boolean insert(Object bean) {
		boolean success = false;
		DBConnect dbc = null;
		//System.out.print(ObjectRelationMappingUtil.getObjectInsertSQL((DocRelation)bean));
		try {
			dbc =  new DBConnect(ObjectRelationMappingUtil.getObjectInsertSQL((DocRelation)bean));
			dbc.executeUpdate();
			success = true;
		}catch(Exception e){
			System.out.println("insert() Exception in "+this.getClass()+":" + e.getMessage());
		}finally{
			if(dbc!=null)
				dbc.close();
		}
		return success;
	}

	@Override
	public boolean update(Object bean) {
		boolean success = false;
		DBConnect dbc = null;
		try{
			dbc = new DBConnect(ObjectRelationMappingUtil.getObjectUpdateSQL((DocRelation)bean, 
					"mainDocId= "+((DocRelation)bean).getMainDocId() +" and relatedDocId = "+((DocRelation)bean).getRelatedDocId()));
			dbc.executeUpdate();
			success = true;
		}catch(Exception e){
			System.out.println("update() Exception in "+this.getClass()+":"+e.getMessage());
		}finally{
			if(dbc!=null)
				dbc.close();
		}
		return success;
	}
	
	public  ArrayList<DocRelation> getListByMainDocID(int docID){
		ArrayList<DocRelation> relationList = new ArrayList<DocRelation>();
		String sql = "select * from docrelation where mainDocId= ?";
		DBConnect dbc = null;
		try {
			dbc =  new DBConnect(sql);
			dbc.setInt(1, docID);
			ResultSet rs = dbc.executeQuery();
			while(rs.next()){
				DocRelation relation = new DocRelation();
				ObjectRelationMappingUtil.fillinBO(relation,rs);
				relationList.add(relation);
			}
		}catch(Exception e){
			System.out.println("getListByMainDocID(int docID) Exception in "+this.getClass()+":" + e.getMessage());
		}finally{
			if(dbc!=null)
				dbc.close();
		}
		
		return relationList;
	}
	
	public  ArrayList<DocRelation> getListByRelayDocID(int docID){
		ArrayList<DocRelation> relationList = new ArrayList<DocRelation>();
		String sql = "select * from docrelation where relatedDocId= ?";
		DBConnect dbc = null;
		try {
			dbc =  new DBConnect(sql);
			dbc.setInt(1, docID);
			ResultSet rs = dbc.executeQuery();
			while(rs.next()){
				DocRelation relation = new DocRelation();
				ObjectRelationMappingUtil.fillinBO(relation,rs);
				relationList.add(relation);
			}
		}catch(Exception e){
			System.out.println("getListByRelayDocID(int docID)Exception in "+this.getClass()+":" + e.getMessage());
		}finally{
			if(dbc!=null)
				dbc.close();
		}
		
		return relationList;
	}

	public boolean isExitSomeRelation(int mainDocID , int relyDocID , String relations){
		boolean flag = false;
		String sql = "select * from docrelation where mainDocId = ? and relatedDocId = ? and relationType = ?";
		DBConnect dbc = null;
		try {
			dbc =  new DBConnect(sql);
			dbc.setInt(1, mainDocID);
			dbc.setInt(2, relyDocID);
			dbc.setString(3, relations);
			ResultSet rs = dbc.executeQuery();
			if(rs.next()){
				flag = true;
			}
		}catch(Exception e){
			System.out.println("isExitSomeRelation(int mainDocID , int relyDocID , String relations) Exception in "+this.getClass()+":" + e.getMessage());
		}finally{
			if(dbc!=null)
				dbc.close();
		}
		return flag;
	}
}
